import { getSkuDiffNum } from '@/api/mengli';
import { hasRouteAuth } from '@/utils/permission';

export const tabListData = [
  { id: '0', name: '萌力库存', routerName: 'mengliInventory', isHidden: !hasRouteAuth('mengli:inventory:read') },
  { id: '1', name: '入库记录', routerName: 'mengliInventoryLog', isHidden: !hasRouteAuth('mengli:inventoryLog:read') },
  { id: '2', name: 'SKU记录', routerName: 'mengliSkuLog', isHidden: !hasRouteAuth('mengli:skuLog:read'), num: 0 },
  { id: '3', name: '结算记录', routerName: 'mengliSettleAccountsLog', isHidden: !hasRouteAuth('mengli:settleAccountsLog:read') }
];

export const handleTabChange = (id, self) => {
  self.activeId = id;
  const name = tabListData.find(v => v.id === id).routerName;
  self.$router.push({
    name
  });
};

// 获取差异SKU编码数量
export const getSkuDiffNumFunc = () => {
  if (hasRouteAuth('mengli:skuLog:read')) {
    getSkuDiffNum().then(res => {
      if (res.code === 200 && res.data) {
        tabListData[2].num = res.data;
      }
    });
  }
};

export const pickerOptions = () => {
  return {
    shortcuts: [{
      text: '本周',
      onClick(picker) {
        const now = new Date();
        const nowTime = now.setHours(0, 0, 0, 0);
        const day = now.getDay();
        const oneDayTime = 24 * 60 * 60 * 1000;
        const MondayTime = nowTime - (day - 1) * oneDayTime;// 显示周一
        const SundayTime = (nowTime + oneDayTime - 1) + (7 - day) * oneDayTime;// 显示周日
        picker.$emit('pick', [new Date(MondayTime), new Date(SundayTime)]);
      }
    }, {
      text: '上周',
      onClick(picker) {
        const now = new Date();
        const nowTime = now.setHours(0, 0, 0, 0);
        const day = now.getDay();
        const oneDayTime = 24 * 60 * 60 * 1000;
        const MondayTime = nowTime - (day + (7 - 1 - 0)) * oneDayTime;// 显示上周一
        const SundayTime = (nowTime + oneDayTime - 1) - (day + (7 - 1 - 6)) * oneDayTime;// 显示上周日
        picker.$emit('pick', [new Date(MondayTime), new Date(SundayTime)]);
      }
    }, {
      text: '本月',
      onClick(picker) {
        const now = new Date();
        const year = now.getFullYear();
        const month = now.getMonth();
        const start = new Date(year, month, 1, 0, 0, 0);
        const end = new Date(new Date(year, month + 1, 1).setHours(0, 0, 0, 0) - 1);
        picker.$emit('pick', [start, end]);
      }
    }, {
      text: '上月',
      onClick(picker) {
        const now = new Date();
        const year = now.getFullYear();
        const month = now.getMonth();
        const start = new Date(year, month - 1, 1, 0, 0, 0);
        const end = new Date(new Date(year, month, 1).setHours(0, 0, 0, 0) - 1);
        picker.$emit('pick', [start, end]);
      }
    }, {
      text: '本年',
      onClick(picker) {
        const now = new Date();
        const year = now.getFullYear();
        const start = new Date(year, 0, 1, 0, 0, 0);
        const end = new Date(new Date(year + 1, 0, 1).setHours(0, 0, 0, 0) - 1);
        picker.$emit('pick', [start, end]);
      }
    }, {
      text: '去年',
      onClick(picker) {
        const now = new Date();
        const year = now.getFullYear();
        const start = new Date(year - 1, 0, 1, 0, 0, 0);
        const end = new Date(new Date(year, 0, 1).setHours(0, 0, 0, 0) - 1);
        picker.$emit('pick', [start, end]);
      }
    }]
  };
};

